package org.batatasframework.values;

import org.springframework.util.Assert;

/**
 * 范围，即这个值在这个范围内
 * @author jackson
 *
 */
public class RangeValue {
	
	public static final char DEFAULT_SEPARATOR = '~';
	
	private Number max, min;
	
	public RangeValue(Number max, Number min){
		this.max = max;
		this.min = min;
	}
	
	public RangeValue(String value) throws Exception{
		this(value, DEFAULT_SEPARATOR);
	}
	
	public RangeValue(String value, char separator) throws Exception{
		int pointOf = value.indexOf(separator);
		Assert.isTrue(pointOf >= 0, "表达式格式不对:" + value);
		
		if(pointOf > 0){
			min = Double.parseDouble(value.substring(0, pointOf));
		}
		
		pointOf += 1;
		if(pointOf < value.length()){
			max = Double.parseDouble(value.substring(pointOf));
		}
	}
	
	public int intMax(){
		if(null == max) return Integer.MAX_VALUE;
		return max.intValue();
	}
	
	public long longMax(){
		if(null == max) return Long.MAX_VALUE;
		return max.longValue();
	}
	
	public double doubleMax(){
		if(null == max) return Double.MAX_VALUE;
		return max.doubleValue();
	}
	
	public int intMin(){
		if(null == min) return Integer.MIN_VALUE;
		return min.intValue();
	}
	
	public long longMin(){
		if(null == min) return Long.MIN_VALUE;
		return min.longValue();
	}
	
	public double doubleMin(){
		if(null == min) return Double.MIN_VALUE;
		return min.doubleValue();
	}
	
	public boolean in(double value){
		return max.doubleValue() > value && value > min.doubleValue();
	}
	public boolean in(int value){
		return max.intValue() > value && value > min.intValue();
	}
	public boolean in(long value){
		return max.longValue() > value && value > min.longValue();
	}
}
